(function(factory) {
<<<<<<< HEAD
	var golbal = window;
	if (typeof define === "function" && define.amd) {
		define([], factory(golbal));
	} else {
		factory(golbal);
	}
})(function(golbal) {
	var stateList = ["pending", "fulfilled", "rejected"],
		noop = function() {};
	var Promise = function(mixin) {
		var thens = [];

		var pe = {
			state: stateList[0],
			resolve: function() {
				_fire.call(this, "success", arguments);
			},
			reject: function() {
				_fire.call(this, "fail", arguments);
			},
			notify: function() {
				_fire.call(this, "process", arguments);
			},
			//promise核心函数，定义处理的回调函数
			then: function(onSuccess, onFail, onProcess) {

				if (!onSuccess || typeof onSuccess !== "function") {
					throw new Error("需要成功回调函数");
				}
				var npe = Promise();
				mix(npe, {
					'success': onSuccess,
					'fail': onFail || noop,
					'process': onProcess || noop
				}, true);
				thens.push(npe);
				return this;
			},
			promise:function(){
				return unmount(this,["resolve","reject","notify"]) ;
			}
		},
			_fire = function(method, args) {
				var index = findIndex(stateList,this.state),ret;
				if (index > 0) {
					throw new Error("状态已完成，无法再次改变");
				}
				index = method === "success" ? 1 : medthod === "fail" ? 2 : 0;
				this.state = stateList[index];

				thens.forEach(function(i, then) {
					if(ret){//如果有返回值，则加入回调参数，传递给下一个then的回调处理函数中
						args = Array.prototype.slice.call(args).concat(ret);
					}
					ret = then[method].apply(this, args);
					
				});
			};
			
		if (mixin && typeof mixin === "function") {
			mixin(pe);
		} else if (mixin && typeof mixin === "object") {
			mix(pe, mixin);
		}

		return pe;
	};


	
	Array.prototype.forEach = function(cbk) {
		var that = this,
			len = that.length,
			i = 0;
		for (; i < len; i++) {
			if (false === cbk.call(that[i], i, that[i])) {
				return;
			}
		}
	};
	//找到第一次出现的下标
	var findIndex = function(list,obj) {
		var len = list.length,
			i = 0;
		for (; i < len; i++) {
			if (len[i] === obj) {
				return i;
			}
		};
	};
	//混合属性deep表示是否覆盖
	function mix(target, extra, deep) {
		for (var k in extra) {
			if (target[k] && deep) {
				target[k] = extra[k];
			} else {
				target[k] = extra[k];
			}

		}
	}
	//删除属性
	function unmount(target,attrList){
		attrList.forEach(function(i,attr){
			delete target[attr];
		})
		return target;
	}

	return golbal.Promise = Promise;
=======
    var golbal = this;
    if (typeof define === "function" && define.amd) {
        define([], factory(golbal));
    } else {
        factory(golbal);
    }
})(function(golbal) {
    var stateList = ["pending", "fulfilled", "rejected"],
        noop = function() {};
    var Promise = function(mixin) {
        var thens = [];
 
        var pe = {
            state: stateList[0],
            resolve: function() {
                _fire.call(this, "success", arguments);
            },
            reject: function() {
                _fire.call(this, "fail", arguments);
            },
            notify: function() {
                _fire.call(this, "process", arguments);
            },
            //promise核心函数，定义处理的回调函数
            then: function(onSuccess, onFail, onProcess) {
 
                if (!onSuccess || typeof onSuccess !== "function") {
                    throw new Error("需要成功回调函数");
                }
                var npe = Promise();
                mix(npe, {
                    'success': onSuccess,
                    'fail': onFail || noop,
                    'process': onProcess || noop
                }, true);
                thens.push(npe);
                return this;
            },
            promise:function(){
                return unmount(this,["resolve","reject","notify"]) ;
            }
        },
            _fire = function(method, args) {
                var index = findIndex(stateList,this.state),ret;
                if (index > 0) {
                    throw new Error("状态已完成，无法再次改变");
                }
                index = method === "success" ? 1 : medthod === "fail" ? 2 : 0;
                this.state = stateList[index];
 
                thens.forEach(function(i, then) {
                    if(ret){//如果有返回值，则加入回调参数，传递给下一个then的回调处理函数中
                        args = Array.prototype.slice.call(args).concat(ret);
                    }
                    ret = then[method].apply(this, args);
                     
                });
            };
             
        if (mixin && typeof mixin === "function") {
            mixin(pe);
        } else if (mixin && typeof mixin === "object") {
            mix(pe, mixin);
        }
 
        return pe;
    };
      
    Array.prototype.forEach = function(cbk) {
        var that = this,
            len = that.length,
            i = 0;
        for (; i < len; i++) {
            if (false === cbk.call(that[i], i, that[i])) {
                return;
            }
        }
    };
    //找到第一次出现的下标
    var findIndex = function(list,obj) {
        var len = list.length,
            i = 0;
        for (; i < len; i++) {
            if (len[i] === obj) {
                return i;
            }
        };
    };
    //混合属性deep表示是否覆盖
    function mix(target, extra, deep) {
        for (var k in extra) {
            if (target[k] && deep) {
                target[k] = extra[k];
            } else {
                target[k] = extra[k];
            }
 
        }
    }
    //删除属性
    function unmount(target,attrList){
        attrList.forEach(function(i,attr){
            delete target[attr];
        })
        return target;
    }
 
    return golbal.Promise = Promise;
>>>>>>> 49a879c4aeae4b0970cceb23edb746625c5b803e
})